//
//  UIViewController+Nav.m
//  jtw_iOS
//
//  Created by zhangzhenyun on 2018/3/6.
//  Copyright © 2018年 Beijing Youjiu Network. All rights reserved.
//

#import "UIViewController+Nav.h"


@implementation UIViewController (Nav)
- (void)addLeftItem {
    UIButton * buttonLeft=[UIButton buttonWithType:UIButtonTypeCustom];
    [buttonLeft setImage:[UIImage imageNamed:@"nav_back"] forState:UIControlStateNormal];
    [buttonLeft addTarget:self action:@selector(leftItemClick:) forControlEvents:UIControlEventTouchUpInside];
    buttonLeft.frame = CGRectMake(0, 0, 44, 44);
//    buttonLeft.contentHorizontalAlignment=UIControlContentHorizontalAlignmentLeft;
    buttonLeft.imageEdgeInsets = UIEdgeInsetsMake(0, -35, 0, 0);
    UIBarButtonItem * leftItem =[[UIBarButtonItem alloc]initWithCustomView:buttonLeft];
    self.navigationItem.leftBarButtonItem = leftItem;
}
- (void)addRightItemTitle:(NSString *)title titleColor:(UIColor *)titleColor {
    UIButton * buttonLeft=[UIButton buttonWithType:UIButtonTypeCustom];
    [buttonLeft setTitle:title forState:UIControlStateNormal];
    [buttonLeft addTarget:self action:@selector(rightItemClick:) forControlEvents:UIControlEventTouchUpInside];
    [buttonLeft setTitleColor:titleColor forState:UIControlStateNormal];
    buttonLeft.titleLabel.font = [UIFont systemFontOfSize:15.0f];
    buttonLeft.frame = CGRectMake(0, 0, 44,44);
    buttonLeft.contentHorizontalAlignment=UIControlContentHorizontalAlignmentLeft;
    UIBarButtonItem * leftItem =[[UIBarButtonItem alloc]initWithCustomView:buttonLeft];
    self.navigationItem.rightBarButtonItem = leftItem;
}
-(void)addRigntItemWithImage:(NSString *)imgName {
    UIButton * rightBtn=[UIButton buttonWithType:UIButtonTypeCustom];
    [rightBtn setImage:[UIImage imageNamed:imgName] forState:UIControlStateNormal];
    [rightBtn addTarget:self action:@selector(rightItemClick:) forControlEvents:UIControlEventTouchUpInside];
    rightBtn.frame = CGRectMake(0, 0, Fix375(44),Fix375(44));
    rightBtn.contentHorizontalAlignment=UIControlContentHorizontalAlignmentLeft;
    UIBarButtonItem * rightItem =[[UIBarButtonItem alloc]initWithCustomView:rightBtn];
    self.navigationItem.rightBarButtonItem = rightItem;
}
- (void)leftItemClick:(UIButton *)sender {
    [self.navigationController popViewControllerAnimated:YES];
}
- (void)rightItemClick:(UIButton *)sender {
    
}
//显示navLine
- (void)showNavLine {
    [self showNavline:YES];
}
//去掉navBar顶部线条
- (void)hiddenNavLine {
     [self showNavline:NO];
}
/**设置不透明*/
-(void)setNavBarOpaque {
    self.navigationController.navigationBar.translucent = NO;
}
/**把导航栏设置为全透明效果*/
- (void)setNavBarTransparent {
    [self.navigationController.navigationBar setTranslucent:YES];
    [self.navigationController.navigationBar setBackgroundImage:[UIImage imageWithColor:[UIColor clearColor] withSize:CGSizeMake(1, 1)] forBarMetrics:UIBarMetricsDefault];
    self.navigationController.navigationBar.barStyle =  UIBarStyleBlackTranslucent;
    [self.navigationController.navigationBar setShadowImage:[[UIImage alloc] init]];
}
- (void)setNavSeperatorLineColor:(UIColor *)color hidden:(BOOL)hidden{
    UIView *line = [[UIView alloc] initWithFrame:CGRectMake(0, self.navigationController.navigationBar.height - ONEPIXER, SCREEN_WIDTH, ONEPIXER)];
    line.backgroundColor = color;
    
    if (hidden) {
        for (UIView *subView in self.navigationController.navigationBar.subviews) {
            if (subView.height == ONEPIXER) {
                [subView removeFromSuperview];
            }
        }
    } else {
        [self.navigationController.navigationBar addSubview:line];
    }
}
/** 是否展示导航栏下边的线 */
- (void)showNavline:(BOOL)show {
    NSString *className = @"_UINavigationBarBackground";
    if (@available(ios 10, *)) {
        className = @"_UIBarBackground";
    }
    
    for (UIView *subView in self.navigationController.navigationBar.subviews) {
        if ([subView isKindOfClass:NSClassFromString(className)]) {
            for (UIView *view in subView.subviews) {
                if ([view isKindOfClass:[UIImageView class]] ) {
                    view.hidden = !show;
                }
            }
        }
    }
}


- (void)setNavigationBackGroundColor:(UIColor *)color {
    self.navigationController.navigationBar.barTintColor = color;
}

- (void)setNavigationItemFont:(UIFont *)font color:(UIColor *)color {
    [self.navigationController.navigationBar setTitleTextAttributes:@{NSForegroundColorAttributeName:color,NSFontAttributeName:font}];
    self.navigationController.navigationBar.tintColor = color;
}
/** 解决scrollview自动偏移问题*/
- (void)adjustsScrollViewInsets:(UIScrollView *)scrollView {
    self.extendedLayoutIncludesOpaqueBars = YES;
    if (@available(iOS 11.0, *)) {
        scrollView.contentInsetAdjustmentBehavior = UIScrollViewContentInsetAdjustmentNever;
    } else {
        self.automaticallyAdjustsScrollViewInsets = NO;
    }
}
@end
